package com.ruoyi.cms.utils;

import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.exception.BusinessException;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Component("cmsutil")
public class CmsUtil {

    /**
     * 校验ID串
     * @param ids
     * @param errObject
     * @return
     */
    public static List<Long> checkIds(String ids, String errObject) {
        errObject = StrUtil.isEmpty(errObject) ? "ID" : errObject;
        if (StrUtil.isEmpty(ids)) {
            throw new BusinessException(errObject + "不能为空");
        }
        if (!ReUtil.isMatch("^\\d+(,\\d+)*$", ids)) {
            throw new BusinessException(errObject + "格式错误");
        }
        return Arrays.stream(ids.split(",")).map(Long::valueOf).collect(Collectors.toList());
    }
}
